var work={
	ddressNum:1,//地址栏个数
	dataAll:"",//总数据
	strAll:[],//初始select数据
	addressDivId:1,//当前操作地址uid
	addressCount:1,//当前地址数
	addressIndex:'',//弹窗索引
	/*·····························初始化························*/
	init:function(){
		this.addressLink();
		this.eventListen();
		$("#tel").val(info.tel);
		this.getCommunityData();
		this.getSelect();
		// this.formsub();
	},
	/*·····························DOM························*/
	addressLink:function(){
		$.post("/index.php?m=Admin&c=Customer&a=read&id="+info.id,function(data){
			 console.log(data);
			 var familyData=data.data.families;
			 for(var i=0;i<familyData.length;i++){
			 	var k=i+1;
			 	$(".add-"+k).find(".addressInput").val(familyData[i].address);
			 	$(".add-"+k).find(".familyId").val(familyData[i].id);
			 	$(".add-"+k).find(".customerName").val(data.data.name);
			 	$(".add-"+k).show();
			 }
		});
	
	},
	eventListen:function(){
		// 表单提交
		$(document).on("click","#formSub",function(){
			work.formsub();
		});
		// 删除
		$(document).on("click",".addDel",function(){
			
				work.addressDivId=$(this).attr("uid");
				var familyId=$(".add-"+work.addressDivId).find(".familyId").val();
				var customerId=info.id;
				$.post("/index.php?m=Admin&c=FamilyMember&a=delete",{family_id:familyId,customer_id:customerId},function(data){
					if(data.stat==1){
						$(".add-"+work.addressDivId).hide();
						$(".add-"+work.addressDivId).find(".addressInput").val('');
						$(".add-"+work.addressDivId).find(".customerId").val('');
						$(".add-"+work.addressDivId).find(".customerName").val('');
						$(".add-"+work.addressDivId).find(".customerId").val('');
					}
				});
			
		})
		// 地址栏目添加
		$(document).on("click","#addressAddCol",function(){
			work.addressCount++;
			$(".add-"+work.addressCount).show();
		});
		// 地址编辑
		$(document).on("click",".addEdit",function(){
			work.addressDivId=$(this).attr("uid");
			work.addressIndex=layer.open({
				type:1,
				shade:false,
				title:false,
				area:['300px', '400px'],
				content:$("#addressTpl"),
				cancel:function(index){
				}
			});
		});
	  	//    第一级
	    $(".xiaoqu").change(function () {
	    
	//如果下一级元素有大于2个option那么就重置后续select
	        if($(".quyu").children('option').length>1){
	            $(this).nextAll().each(function(){
	                $(this).empty();
	            });
	           for(var i=0;i<work.strAll.length;i++){
	            $(this).nextAll().eq(i).html(work.strAll[i]);
	            } 
	        }
	        
	        info.xiaoquId = $(this).val();
	        console.log(info.xiaoquId);
	        if(work.strLength(work.dataAll[info.xiaoquId].data)>0){
	            var zones=work.dataAll[info.xiaoquId].data;
	            console.log(zones);
	            work.strAdd(zones,'quyu','zone');
	        }else{
	             work.msg('quyu','区域');
	             var build=work.dataAll[info.xiaoquId][0].data;
	             work.strAdd(build,'louhao');
	        }
	        
	    });
	    //    第二级
	    $(".quyu").change(function(){
	          info.quyuId = $(this).val();
	        var build=work.dataAll[info.xiaoquId]['data'][info.quyuId].data;
	        work.strAdd(build,'louhao','building');
	    });
	    //    第三级
	    $(".louhao").change(function(){
	          info.louhaoId = $(this).val();
	          var unit=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId].data;
	          console.log(unit);
	          work.strAdd(unit,'danyuan','unit');
	          for(x in unit){
	              if(unit[x]['unit_id']==0){
	                  $(".danyuan").find('option').eq(1).text('无单元');
	                  var room=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId]['data'][0].data;
	                   work.strAdd(room,'fangjian','room');
	              }
	          }
	    });
	        //    第四级
	    $(".danyuan").change(function(){
	         info.danyuanId = $(this).val();
	         if(info.danyuanId){
	             var room=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId]['data'][info.danyuanId].data;
	            work.strAdd(room,'fangjian','room');
	         }
	    });
	  	// 确定按钮
	  	$(document).on("click",".addressCon",function(){
	  		var customerId=$(".add-"+work.addressDivId).find(".customerId").val();
	  		var familyId=$(".add-"+work.addressDivId).find(".familyId").val();
	  		var customerName=$(".add-"+work.addressDivId).find(".customerName").val();
	  		var communityId=$(".xiaoqu").find("option:selected").val();
	  		var communityName=$(".xiaoqu").find("option:selected").text();
	  		var zoneId=$(".quyu").find("option:selected").val();
	  		var zoneName=$(".quyu").find("option:selected").text();
	  		var buildId=$(".louhao").find("option:selected").val();
	  		var buildName=$(".louhao").find("option:selected").text();
	  		var unitId=$(".danyuan").find("option:selected").val();
	  		var unitName=$(".danyuan").find("option:selected").text();
	  		var roomId=$(".fangjian").find("option:selected").val();
	  		var roomName=$(".fangjian").find("option:selected").text();
	  		work.addressPost(communityId,customerId,zoneId,unitId,buildId,roomId,customerName,familyId);
	  		work.addressShow(communityName,zoneName,buildName,unitName,roomName);
	  	});
			
	},
	/*·····························功能性函数························*/
	// 地址确定提交数据
	addressPost:function(communityId,customerId,zoneId,unitId,buildId,roomId,customerName,familyId){
		if(familyId){
				 $.ajax({
			        url:"/index.php?m=Admin&c=Family&a=update",
			        type:"POST",
			        data:{
			            "room_id":roomId,
			            "unit_id":unitId,
			            "building_id":buildId,
			            "zone_id":zoneId,
			            "community_id":communityId,
			            "customer_id":info.id,
			            "id":familyId,
			            "customer_name":customerName
			        },
			        error:function(){
			            console.log("服务器繁忙");
			        },
			        success:function(data,status){
						console.log(data);
			            if(data.stat==1){
			            	layer.msg("修改成功",{time:1000},function(){
			            		layer.close(work.addressIndex);
			            	}); 
			            }else{
			            	layer.msg(data.msg);
			            }
			        }
			    });
	    	}else{
	    		$.ajax({
			        url:"/index.php?m=Admin&c=Family&a=add",
			        type:"POST",
			        data:{
			            "room_id":roomId,
			            "unit_id":unitId,
			            "building_id":buildId,
			            "zone_id":zoneId,
			            "community_id":communityId,
			            "customer_id":info.id,
			            "id":familyId,
			            "customer_name":customerName
			        },
			        error:function(){
			            console.log("服务器繁忙");
			        },
			        success:function(data,status){
						console.log(data);
			           	if(data.stat==1){
			           		$(".add-"+work.addressDivId).find(".familyId").val(data.pk);
			           		layer.msg("添加成功",{time:1000},function(){
			           			layer.close(work.addressIndex);
			           		});
			           	}else{
			           		layer.msg(data.msg);
			           	}
			        }
			    });
	    	}
	    	
	},
	// 地址确定显示中文
	addressShow:function(communityName,zoneName,buildName,unitName,roomName){
		var str=communityName+zoneName+buildName+unitName+roomName;
		$(".add-"+work.addressDivId).find(".addressInput").val(str);
	},
	// 五级联动
	//获取数据并设置小区下拉
	getCommunityData:function(){
		 $.get("/index.php?m=Admin&c=House&a=json",function(data){
            // var data=$.parseJSON(data);
            work.dataAll=data.data;
            data=data.data;
            console.log(data);
            // console.log(data);
            var str="";
            for(x in data){
                console.log(data[x]['community_name']);
                if(data[x]['community_name']){
                    
                     str+='<option value="'+data[x]['community_id']+'">'+data[x]['community_name']+'</option>';
                }
               
            }
            $(".xiaoqu").append(str);
        });
	},
	// 获取初始select的值
	getSelect:function(){
		 $(".xiaoqu").nextAll().each(function(){
            work.strAll.push($(this).html());
            });
	},
	 // 计算数组或对象的长度
    strLength:function(str){
        t=typeof(str);
        if(t=='string'){
            return str.length;
        }else if(t=='object'){
            var n=0;
            for(var i in str){
                n++
            }
            return n;
        }
     },
      // 下拉菜单字符串生成
    // data为数据，id为id索引值
    strAdd:function(data,id,key){
        var str="";
        for (x in data){
            if(data[x]['unit_id']==0){
                console.log('无单元号');
                 work.msg('danyuan','单元');
                 // break;
            }
            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
        }
        switch(id){
            case 'xiaoqu':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择小区</option>');
                break;
            case 'quyu':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择区域</option>');
                break;
            case 'louhao':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择楼号</option>');
                break;
            case 'danyuan':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择单元</option>');
                break;
            case 'fangjian':
                $("."+id).empty();
                $("."+id).append('<option value="" selected="true" disabled="true">请选择房间号</option>');
                break;
        }
        $("."+id).append(str);
    },
      // 没有下一级属性时显示的信息
    msg:function(obj,id,msg){
        $(obj).next("."+id).find("option").text('无'+msg);
    },
	/*·····························插件初始化························*/
	formsub:function(){
		        $("#form-customer-add").ajaxSubmit({
		        type:'POST',
		        url:'/index.php?m=Admin&c=Customer&a=update',
		        success:function(data){
		          console.log(status);
		        	if(data.stat==1){
		              layer.msg("修改成功");
		              var index = parent.layer.getFrameIndex(window.name);
		              setTimeout(function(){
		                   parent.layer.close(index);
		              },1000);
		              // parent.location.reload();
		            parent.window.location.href="/index.php?m=Service&c=Center&a=index&tel="+info.tel;
		          }else{
		            layer.msg(data.msg);
		          }
		          }
		        });
	}
}